﻿using System;
using System.Collections.Generic;
using System.Text;
using CommunityServer.WebServices.MediaGalleries;
using CommunityServer.WebServices.Membership;
using CommunityServer.WebServices.Common;
using System.Windows.Forms;

namespace nb.development.csmediamanager
{
    public class GalleryWorker
    {
        public GalleryWorker(Guid siteid)
        {
            this.siteid = siteid;
        }

        public GalleryWorker(Guid siteid, MediaGallery gallery)
        {
            this.siteid = siteid;
            this.gallery = gallery;
        }

        private Guid siteid;
        private MediaGallery gallery;
        public event GalleryWorkerCompleted Completed;
        public event GalleryWorkerUpdateCompleted UpdateCompleted;

        public void Run()
        {
            Dictionary<int, MediaGallery> galleries = new Dictionary<int, MediaGallery>();
            Dictionary<int, MediaGalleryGroup> groups = new Dictionary<int, MediaGalleryGroup>();
            //try
            //{
                MediaGalleriesService mgs = Sites.Instance[siteid].Service;
                foreach (MediaGalleryGroup group in mgs.GetMediaGalleryGroups())
                {
                    groups.Add(group.Id, group);
                    foreach (MediaGallery gallery in mgs.GetMediaGalleries(group.Id))
                        if (gallery.Settings.Enabled.Value)
                            galleries.Add(gallery.Id, gallery);
                }
            //}
            //catch (Exception ex) { MessageBox.Show("Error:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            Completed(siteid, galleries, groups);

        }

        public void Update()
        {
            try
            {
                this.gallery.MediaGalleryService = Sites.Instance[siteid].Service;
                this.gallery.Update();
            }
            catch (Exception ex) { MessageBox.Show("Error:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            UpdateCompleted(siteid, this.gallery);
        }

    }
    public delegate void GalleryWorkerCompleted(Guid siteid, Dictionary<int, MediaGallery> galleries, Dictionary<int, MediaGalleryGroup> groups);
    public delegate void GalleryWorkerUpdateCompleted(Guid siteid, MediaGallery gallery);
}
